package io.xxx.xbutler.core.get;

import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.client.PromotionClient;
import io.xxx.xbutler.domain.Jigsaw;
import io.xxx.xbutler.domain.task.PushContext;
import io.xxx.xbutler.domain.task.Receiver;
import io.xxx.xbutler.domain.task.TaskItem;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class ProductJigsawGetter implements ContentGetter {

    @Resource
    private PromotionClient promotionClient;

    @Override
    public void replaceContent(PushContext context) {
        Receiver receiver = context.getReceiver();
        Long storeId = receiver.getStoreId();
        List<TaskItem> taskItems = context.getTaskItems();
        for (TaskItem taskItem : taskItems) {
            if (!taskItem.isProduct() || storeId == null) {
                continue;
            }

            JSONObject content = taskItem.getContent();
            String skuSn = content.getString("skuSn");
            Jigsaw productJigsaw = promotionClient.getProductJigsaw(storeId, skuSn);
            if (productJigsaw == null) {
                log.warn("查询商品拼图为空[taskId:{}, storeId:{}, storeCode:{}, skuSn:{}]",
                        taskItem.getTaskId(), storeId, receiver.getStoreCode(), skuSn);
            } else {
                content.put("thumbUrl", productJigsaw.getThumbUrl());
                taskItem.setContent(content);
            }
        }
    }
}
